const formatTime = (date, symbol1, symbol2) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute, second].map(formatNumber).join(symbol2 || ':')
}

const formatTime2 = (date, symbol1, symbol2) => {
  if (!date) {
    return '';
  }
  date = new Date(date);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join(symbol1 || '-') + ' ' + [hour, minute, second].map(formatNumber).join(symbol2 || ':')
}

const formatTime3 = (date, symbol1, symbol2) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join(symbol1 || '-');
}

const formatTime_1 = (date, symbol1, symbol2) => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()

  return [month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute].map(formatNumber).join(symbol2 || ':')
}

const formatTime_2 = (date, symbol1, symbol2) => {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join(symbol1 || '/');
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const dealObjectValue = (obj) => {
  let param = {};
  console.log(obj)
  if (obj === null || obj === undefined || obj === "") return param;
  for (let key in obj) {
    if (typeof (obj[key]) === "Object") {
      param[key] = dealObjectValue(obj[key]);
    } else if (obj[key] !== null && obj[key] !== undefined && obj[key] !== "") {
      param[key] = obj[key];
    }
  }
  return param;
}

const testidCard = n => {
  if ((/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(n))) {
    return false;
  } else {
    return true;
  }
}
const formatDate = date => {
  console.log(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('/')
}
const IdCard = (UUserCard, num) => {
  if (num == 1) { //获取出生日期    
    var birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
    return birth;
  }
  if (num == 2) { //获取性别    
    if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) { //男      
      return "男";
    } else { //女      
      return "女";
    }
  }
  if (num == 3) { //获取年龄    
    var myDate = new Date();
    var month = myDate.getMonth() + 1;
    var day = myDate.getDate();
    var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
    if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
      age++;
    }
    return age;
  }
}


/**
 * 确认提示框
 */
const confirm = (content) => {
  return new Promise(function (resolve, reject) {
    wx.showModal({
      content: content,
      success: function (res) {
        if (res.confirm) {
          resolve(true);
        } else {
          reject(false);
        }
      }
    })
  });
}


const limitMoney = (value) => {
  value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
  value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
  value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
  value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
  if (value.indexOf(".") < 0 && value != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
    value = parseFloat(value);
  }

  return value
}

module.exports = {
  formatTime: formatTime,
  formatTime2: formatTime2,
  formatTime3: formatTime3,
  formatTime_1: formatTime_1,
  formatTime_2,
  dealObjectValue,
  testidCard,
  formatDate,
  IdCard: IdCard,
  confirm: confirm,
  limitMoney:limitMoney
}